const koa =require("koa")
const router = require("koa-router")()
const app = new koa()
const koaBody = require('koa-body')
const {dataModel,studentModel} = require("./modles/data")

const cors = require('koa2-cors')
const bodyParser = require("koa-bodyparser")
router.get("/data",async ctx =>{
  console.log(ctx);
  var data = await  dataModel.find();
  // console.log(data);
   ctx.body = {data,"msg":"成功"};
})
router.get("/dataPage",async ctx =>{
  let {start,limit} = ctx.query
  if(start == undefined)
  {start = 0}
  if(limit ==undefined)
  {limit = 5}
  var data = await  dataModel.find().skip(Number(start)).limit(Number(limit));
  var total  = await dataModel.find().count()
   console.log(start,limit);
   console.log(data);
   ctx.body = {
     data,
    "msg":"成功",
     total,
};
})
router.get("/student",async ctx =>{
  var student = await studentModel.find();
  console.log(student);
  ctx.body = student;
})
//插入图书
router.post("/add",async ctx =>{
  console.log(ctx.request.body);
  let {Iname,Idate,Iprice,Iaddress,IisLended} = ctx.request.body
  let data = new  dataModel({
   name:Iname,
   date:Idate,
   price:Iprice,
   address:Iaddress,
   isLended:IisLended
  })
  let n = await dataModel.find({name:Iname})
  if(n.length == 0 && Iname != "")
  {await data.save()
  ctx.body = {msg:"插入成功"}}
 else 
 {
   ctx.body = {msg:"插入失败"}
 }
} )
//借阅图书
router.post("/lend",async ctx =>{
  let {Snum,Bname,Ldate,Bdate}= ctx.request.body
  let book = await dataModel.find({name:Bname})
  console.log(book.length);
  if(book.length!=0)
  {
  if(book[0].isLended =="可借用")
  {let stu = await studentModel.find({num:Snum})
  console.log(stu[0]);
  let data = [{
    name:Bname,
    date1:Ldate,
    date2:Bdate,
  }]
  let n = stu[0].lMsg.concat(data)
  let a = await studentModel.updateOne({num:Snum},{lMsg:n})
  let b = await dataModel.updateOne({name:Bname},{isLended:"不可借用"})
  if(a.nModified && b.nModified)
  {ctx.body = {msg:"借阅成功，请按时归还"}}
  }
  else 
  {
   ctx.body = {msg:"图书已被人借用"}
  }
 }
 else
 {
   ctx.body = {msg:"未找到该图书"}
 }
})
//归还图书
router.post("/back",async ctx =>{
  let {Bname,Snum} = ctx.request.body
  console.log(Bname);
  let a = await dataModel.updateOne({name:Bname},{isLended:"可借用"})
  let b =await studentModel.find({num:Snum})
  console.log(b);
  let c = b[0].lMsg
  console.log(c);
  
  for(let i=0;i<c.length;i++){
    if(c[i].name == Bname)
    {
    c.splice(i,1)
  }
  }
  let d = await studentModel.updateOne({num:Snum},{lMsg:c})
  console.log(a);
  console.log(d);
  if(a.nModified && d.nModified )
  {
    ctx.body = {msg:"还书成功"}
  }
  else{
    ctx.body = {msg:"还书失败"}
  }
})
//删除学生
router.post("/deleteStu",async ctx=>{
  let num = ctx.request.body.num
  console.log(num);
  let a = await studentModel.deleteOne({num:num})
  console.log(a);
  if(a. deletedCount){
    ctx.body = {msg:"删除成功"}
  }
})
app.use(koaBody())
app.use(bodyParser())
app.use(cors())
app.use(router.routes())
app.listen(8080,() =>{
  console.log("8080端口启动");
})